//# sourceURL=http://uMapApp/Module/Maintain/MaintainModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/Item/ListItem"
    , "../../UI/Layer"
    , "./Maintain"
    , "./MaintainBlock"
    , "../../UI/QueryNode"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock,
    ListItem
    , Layer
    , Maintain
    , MaintainBlock
    , QueryNode
) {

    function MaintainModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "道路养护";
        _this.icon = _this.app.icons.maintain;

        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = _this.title;
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.listPanel.listBlock.filter = {
            "param": "maintain.searchKey"
        };

        _this.listPanel.listBlock.sorter = {
            "param": {
                "value": "maintain.sortKey",
                "label": "桩号/车道"
            }
        };

        _this.panels.push(_this.listPanel);

        _this.app.ui.layout.right.append(_this.listPanel);

        _this.layer = new Layer({
            "app": _this.app
        });

        _this.layer.pointPrimitives = _this.layer.primitives.add(new uEarth.PointPrimitiveCollection());
        _this.layer.labelPrimitives = _this.layer.primitives.add(new uEarth.LabelCollection());
        _this.layer.billboardPrimitives = _this.layer.primitives.add(new uEarth.BillboardCollection());


    }

    var _class = MaintainModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"           : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"     : {
            value: function (list) {

                var _this = this;

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;
                    item.icon = _this.icon;
                    item.dic = _this.dic;
                    item.damageTypeDic = _this.damageType;
                    item.module = _this;

                    if (item["车道"] && /第[一二三]车道/.test(item["车道"])) {

                        var roadTrackNo = ["一", "二", "三"].indexOf(item["车道"].charAt(1)) + 1;

                        item.roadTrack = {
                            "no"             : roadTrackNo,
                            "northRoadLineNo": roadTrackNo + (item["朝向"] === "上行" ? -1 : 0),
                            "southRoadLineNo": roadTrackNo + (item["朝向"] === "上行" ? 0 : -1)
                        };

                    }

                    if (item["朝向"] && item["朝向"] !== "-" && item["roadTrack"] && item["桩号"]) {

                        var positionHeading = _this.app.modules.road.roadLineModule.getSampledRoadTrackPositionHeading(
                            {
                                "direction" : item["朝向"],
                                "roadLineNo": item["roadTrack"].northRoadLineNo
                            },
                            {
                                "direction" : item["朝向"],
                                "roadLineNo": item["roadTrack"].southRoadLineNo
                            },
                            item["桩号"]
                        );

                        if (positionHeading) {
                            item.positionHeading = positionHeading;
                        }

                    }


                    var maintain = new Maintain(item);
                    _this.items.push(maintain);

                    maintain.listItem = new ListItem({
                        "title"   : maintain.title,
                        "subtitle": maintain["位置"],
                        "icon"    : _this.icon.base64,
                        "maintain": maintain,
                        "class"   : "maintain"
                    });

                    if (maintain.damageMainType) {
                        maintain.listItem.addClass("type_" + maintain.damageMainType.code);
                    }

                    maintain.block = new MaintainBlock({
                        properties    : maintain,
                        modalityWindow: _this.app.modalityWindow
                    });

                    maintain.openPop = function () {
                        maintain.active();
                        maintain.block.load();
                        maintain.speak();
                        _this.app.speaker.one("end", function () {
                            _this.app.pop.close();
                        });
                        _this.app.pop.target = maintain;
                        _this.app.pop.content = maintain.block;
                        _this.app.pop.position = maintain.position;
                        _this.app.pop.one("close", function () {
                            _this.app.speaker.stop();
                            maintain.active(false);
                            maintain.trigger("closePop");
                        });
                        _this.app.pop.open();
                    };

                    maintain.on("click", function () {
                        _this.activeItem(maintain);
                        maintain.openPop();
                    });

                    maintain.on("close", function (maintain) {
                        if (_this.app.pop.target === maintain) {
                            _this.app.pop.close();
                        }
                    });

                    maintain.listItem.on("click", function () {
                        _this.activeItem(maintain);
                        if (maintain.viewport) {

                            _this.app.earth.camera.flyToBoundingSphere(
                                maintain.viewport.boundingSphere,
                                {
                                    offset: maintain.viewport.hpr
                                });
                        }
                    });

                    maintain.listItem.on("hide", function (listItem) {
                        listItem.maintain.close();
                    });

                    maintain.listItem.on("show", function (listItem) {
                        listItem.maintain.open();
                    });

                    _this.listPanel.listBlock.add(maintain.listItem);

                });

                _this.listPanel.listBlock.filter.list = _this.listPanel.listBlock.list;
                _this.listPanel.listBlock.sorter.list = _this.listPanel.listBlock.list;
            }
        },
        "clear"          : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "objMap"         : {
            set: function (objMap) {
                var _this = this;
                _this._objMap = new mhjs.Map(objMap);
            },
            get: function () {
                return this._objMap;
            }
        },
        "propertiesMap"  : {
            set: function (propertiesMap) {
                var _this = this;
                _this._propertiesMap = new mhjs.Map(propertiesMap);
            },
            get: function () {
                return this._propertiesMap;
            }
        },
        "open"           : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.layer.open();

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);
            }
        },
        "loadDamageType" : {
            value: function (data, callback) {

                var _this = this;
                if (_this.damageTypeRequest) {
                    _this.damageTypeRequest.send(data, function (json) {
                        if (json.data.list && json.data.list.length > 0) {
                            callback && callback(json.data.list);
                        }
                    });
                }
            }
        },
        "load"           : {
            value: function (data, callback) {

                var _this = this;

                var queryNode = _this.app.queryManager.add(new QueryNode({
                    "title"   : "[模块] : " + _this.title,
                    "modality": true
                }));

                queryNode.trigger("send");

                _this.loadMap(function (mapJson) {
                    _this.loadData(data, function (dataJson) {
                        _this.loadDamageType(data, function (damageTypeJson) {
                            _this.isLoaded = true;
                            _this.trigger("load");
                            queryNode.trigger("loaded");
                            callback && callback(dataJson, mapJson, damageTypeJson);

                        });


                    });
                });

            }
        },
        "loadInit"       : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({"year": 2022}, function (data, mapData, damageTypeJson) {

                        _this.app.modules.road.roadLineModule.loadInit(function () {

                            if (mapData) {
                                _this.objMap = mapData.obj;
                                _this.propertiesMap = mapData.properties;
                                _this.dic = mapData.dic;
                            }

                            if (damageTypeJson) {
                                _this.damageType = damageTypeJson;
                            }

                            _this.data = data;
                            _this.isLoaded = true;

                            _this.trigger("loadInit");

                            callback && callback();

                        });
                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"      : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"          : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.layer.close();
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem"     : {
            value: function (item) {
                var _this = this;
                if (!item.isActive) {
                    _this.unActiveAllItem();
                    item.active();
                    item.listItem.addClass("active");
                    item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
                }
            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});